package com.erb.controller.ljy;

import com.erb.entity.Thirdparty;
import com.erb.entity.User;
import com.erb.service.ljy.setUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * ClassName: setUserController
 * Description:账户信息修改的相关操作
 * date: 2020/6/12 14:20
 *
 * @author JY
 */


@Controller
@RequestMapping("/setUser")
public class setUserController {

	int codes = 0;

	@Resource
	private setUserService setUserService;

	@RequestMapping("/toView")
	public String toView(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		Thirdparty thirdparty = setUserService.thisel(user.getId());
		if (thirdparty == null) {
			model.addAttribute("thi", 0);
		} else {
			model.addAttribute("thi", 1);
		}
		User users = setUserService.SearchUserById(user.getId());
		model.addAttribute("user", users);
		//手机号码格式转换
		String phone = users.getPhone();
		phone = phone.substring(0, 3) + "****" + phone.substring(7, 11);
		model.addAttribute("phone", phone);
		//邮箱格式转换
		String email = users.getEmail();
		email = email.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4");
		model.addAttribute("email", email);
		return "personal/setAccounts";
	}

	//生成短信验证码
	@RequestMapping("/smsverification")
	@ResponseBody
	public void smsverification(String phone, HttpSession session) {
		if (phone == "" | phone == null) {
			User user = (User) session.getAttribute("user");
			phone = user.getPhone();
		}
//        Random random = new Random();
//        int i = random.nextInt(999999);  //随机生成6位数字
		int i = 111111;
		codes = i;
//        VerificationCode vc = new VerificationCode();
//        vc.vercode(phone, i);
	}

	//验证生成的短信验证码
	@RequestMapping("/verification")
	@ResponseBody
	public int verification(int yzm) {
		if (yzm == codes) {
			return 1;
		} else {
			return 0;
		}
	}

	//查询手机号是否存在
	@RequestMapping("/selphone")
	@ResponseBody
	public int selphone(String phone, HttpSession session) {
		User user1 = (User) session.getAttribute("user");
		User user = setUserService.phonesel(user1.getId(), phone);
		if (user == null) {
			return 0;
		} else {
			return 1;
		}
	}

	//修改手机号
	@RequestMapping("/updphone")
	@ResponseBody
	public int updphone(String phone, HttpSession session) {
		User user = (User) session.getAttribute("user");
		Integer i = setUserService.phoneupd(user.getId(), phone);
		if (i > 0) {  //修改成功（手机号修改成功后修改session中的user）
			user.setPhone(phone);
			session.setAttribute("user", user);
			return 1;
		} else {  //修改失败
			return 0;
		}
	}

	//验证邮箱是否存在
	@RequestMapping("/selemail")
	@ResponseBody
	public int selemail(String email, HttpSession session) {
		User user1 = (User) session.getAttribute("user");
		User user = setUserService.emailsel(user1.getId(), email);
		if (user == null) {
			return 0;
		} else {
			return 1;
		}
	}

	//修改邮箱
	@RequestMapping("/updemail")
	@ResponseBody
	public int updemail(String email, HttpSession session) {
		User user = (User) session.getAttribute("user");
		Integer i = setUserService.emailupd(user.getId(), email);
		if (i > 0) {  //修改邮箱成功
			user.setEmail(email);
			session.setAttribute("user", user);
			return 1;
		} else {  //修改邮箱失败
			return 0;
		}
	}

	//验证原登录密码是否正确
	@RequestMapping("/selpwd")
	@ResponseBody
	public int selpwd(String pwd, HttpSession session) {
		User user1 = (User) session.getAttribute("user");
		User user = setUserService.pwdsel(user1.getId(), pwd);
		if (user != null) {
			return 1;
		} else {
			return 0;
		}
	}

	//修改登录密码
	@ResponseBody
	@RequestMapping("/updpwd")
	public int updpwd(String xmm, HttpSession session) {
		User user = (User) session.getAttribute("user");
		Integer i = setUserService.pwdupd(user.getId(), xmm);
		if (i > 0) {  //修改成功后退出登录
			session.invalidate();  //清除所有session
			return 1;
		} else {
			return 0;
		}
	}

	//查询原支付密码是否正确
	@ResponseBody
	@RequestMapping("/selthi")
	public int selthi(int pwd, HttpSession session) {
		User user = (User) session.getAttribute("user");
		Thirdparty thi = setUserService.thirsel(user.getId(), pwd);
		if (thi != null) {
			return 1;
		} else {
			return 0;
		}
	}

	//修改支付密码
	@ResponseBody
	@RequestMapping("/updthi")
	public int updthi(int xmm, HttpSession session) {
		User user = (User) session.getAttribute("user");
		Integer i = setUserService.thiupd(user.getId(), xmm);
		if (i > 0) {  //修改成功
			return 1;
		} else {  //修改失败
			return 0;
		}
	}

}
